﻿using System.Collections.Generic;
using System.Linq;
using Abp.Localization;
using FutureApp.EntityFramework;
using System.Xml.Linq;
using System.Reflection;

namespace FutureApp.Migrations.SeedData
{
    public class DefaultLanguagesCreator
    {
        public static List<ApplicationLanguage> InitialLanguages { get; private set; }

        public static List<ApplicationLanguageText> InitialLanguageTexts { get; private set; }

        private readonly FutureAppDbContext _context;

        static DefaultLanguagesCreator()
        {
            InitialLanguages = new List<ApplicationLanguage>
            {
                new ApplicationLanguage(null, "en", "English", "famfamfam-flag-gb"),
                new ApplicationLanguage(null, "zh-CN", "简体中文", "famfamfam-flag-cn")
            };
            InitialLanguageTexts = new List<ApplicationLanguageText>();
            InitialLanguageTexts.AddRange(LoadLanguageSetting("AbpWeb"));
            InitialLanguageTexts.AddRange(LoadLanguageSetting(FutureAppConsts.LocalizationSourceName));
        }

        private static List<ApplicationLanguageText> LoadLanguageSetting(string localizationSource)
        {
            var xdoc = XDocument.Load(Assembly.GetExecutingAssembly().GetManifestResourceStream($"FutureApp.Language.{localizationSource}.xml"));
            var texts = from dict in xdoc.Elements("LocalizationDictionaries")
                        from lang in dict.Elements("LocalizationDictionary")
                        from text in lang.Elements("text")
                        select new ApplicationLanguageText
                        {
                            LanguageName = lang.Attribute("culture").Value,
                            Source = localizationSource,
                            Key = text.Attribute("name").Value,
                            Value = text.Attribute("value").Value
                        };
            return texts.ToList();
        }

        public DefaultLanguagesCreator(FutureAppDbContext context)
        {
            this._context = context;
        }

        public void Create()
        {
            this.CreateLanguages();
        }

        private void CreateLanguages()
        {
            foreach (var language in InitialLanguages)
            {
                this.AddLanguageIfNotExists(language);
            }
            foreach (var text in InitialLanguageTexts)
            {
                this.AddLanguageTextIfNotExists(text);
            }
        }

        private void AddLanguageIfNotExists(ApplicationLanguage language)
        {
            if (this._context.Languages.Any(l => l.TenantId == language.TenantId && l.Name == language.Name))
            {
                return;
            }

            this._context.Languages.Add(language);
            this._context.SaveChanges();
        }

        private void AddLanguageTextIfNotExists(ApplicationLanguageText langText)
        {
            if (this._context.LanguageTexts.Any(l => l.TenantId == langText.TenantId && l.LanguageName == langText.LanguageName && l.Source == langText.Source && l.Key == langText.Key))
            {
                return;
            }
            this._context.LanguageTexts.Add(langText);
            this._context.SaveChanges();
        }
    }
}
